/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Conserto;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//import java.util.Date;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 *
 * @author Rafael Kozar
 */
public class ConsertoDAO {

    private Connection connection;

    public ConsertoDAO() {
        this.connection = new ConnectionFactory().getConnection();
    }

    public void insere(Conserto conserto) throws SQLException {
        String sql = "INSERT INTO conserto (dt_conserto, status, observacao, idPedido) VALUES(?, ?, ?, ?) ";
        PreparedStatement stmt = connection.prepareStatement(sql);
        Calendar cal = Calendar.getInstance();

        GregorianCalendar dataCal = new GregorianCalendar();

        dataCal.setTime(cal.getTime());
        int mes = dataCal.get(Calendar.MONTH);
        int dia = dataCal.get(Calendar.DAY_OF_MONTH);
        int ano = dataCal.get(Calendar.YEAR);
        stmt.setDate(1, new java.sql.Date((ano - 1900), mes, dia));
        stmt.setString(2, conserto.getStatus());
        stmt.setString(3, conserto.getObservacao());
        stmt.setInt(4, conserto.getIdPedido());
        stmt.executeUpdate();
        stmt.close();
    }

    public void altera(Conserto conserto) {
        String sql = "UPDATE conserto SET status = ? WHERE idConserto = ?";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, conserto.getStatus());
            stmt.setInt(2, conserto.getIdConserto());
            stmt.executeQuery();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void finaliza(int idConserto) {
        String sql = "UPDATE conserto SET status = ? WHERE idConserto = ?";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, "finalizado");
            stmt.setInt(2, idConserto);
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Conserto> consertosAbertos() throws SQLException {
        String sql = "SELECT * FROM consertos where status LIKE aberto";
        List<Conserto> consertos = new ArrayList();
        PreparedStatement stmt = connection.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            Conserto con = new Conserto();
            con.setIdConserto(rs.getInt("idConserto"));
            con.setIdPedido(rs.getInt("idPedido"));
            con.setDt_conserto(rs.getDate("dt_conserto"));
            con.setObservacao(rs.getString("observacao"));
            con.setStatus(rs.getString("status"));
            consertos.add(con);
        }
        rs.close();
        stmt.close();
        return consertos;
    }

    public List<Conserto> listaRetira() throws SQLException {
        String sql = "SELECT * FROM conserto WHERE status = ?";
        List<Conserto> consertos = new ArrayList();
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, "retirar");
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            Conserto con = new Conserto();
            con.setIdConserto(rs.getInt("idConserto"));
            con.setIdPedido(rs.getInt("idPedido"));
            con.setDt_conserto(rs.getDate("dt_conserto"));
            con.setObservacao(rs.getString("observacao"));

            consertos.add(con);
        }
        rs.close();
        stmt.close();
        return consertos;
    }

    public List<Conserto> listaFinalizado() throws SQLException {
        String sql = "SELECT * FROM conserto WHERE status = ?";
        List<Conserto> consertos = new ArrayList();
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, "finalizado");
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            Conserto con = new Conserto();
            con.setIdConserto(rs.getInt("idConserto"));
            con.setIdPedido(rs.getInt("idPedido"));
            con.setDt_conserto(rs.getDate("dt_conserto"));
            con.setObservacao(rs.getString("observacao"));

            consertos.add(con);
        }
        rs.close();
        stmt.close();
        return consertos;
    }

}
